package com.yami.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.yami.shop.common.response.ResponseEnum;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.util.SendUtil;
import com.yami.shop.vo.ban.GroupBanVo;
import com.yami.shop.vo.group.GroupIdVo;
import io.rong.RongCloud;
import io.rong.methods.group.Group;
import io.rong.models.Result;
import io.rong.models.group.GroupMember;
import io.rong.models.group.GroupModel;
import io.rong.models.response.ListGagGroupUserResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

import static com.yami.shop.common.response.ServerResponseEntity.fail;
import static com.yami.shop.common.response.ServerResponseEntity.success;

/**
 * 群组整体禁言
 */
@Service
@Slf4j
public class RongYunUserBanServiceImpl {

    @Autowired
    private RongCloud rongCloud;
    private Group group;


    @PostConstruct
    public void init() {
        this.group = rongCloud.group;
    }


    /**
     * 设置用户在加入的所有群组中都不能发送消息
     *
     * @param groupBanVo
     * @return
     */
    public ServerResponseEntity<String> add(GroupBanVo groupBanVo) {
        List<GroupIdVo> groups = groupBanVo.getGroups();
        List<GroupMember> groupMembers = BeanUtil.copyToList(groups, GroupMember.class);
        GroupMember[] groupParam = groupMembers.toArray(new GroupMember[groupMembers.size()]);
        GroupModel param = new GroupModel().setMembers(groupParam).setMinute(groupBanVo.getMinute());
        try {
            Result result = group.ban.user.add(param);
            log.info("group.ban.add-result: {}", result);
            if (SendUtil.requestSuccess(result)) return success();
            return fail(ResponseEnum.EXCEPTION);
        } catch (Exception exception) {
            log.info("group.ban.add-error: {}", exception.getMessage());
            return fail(ResponseEnum.EXCEPTION);
        }
    }

    /**
     * 获取所有群组禁言用户列表
     *
     * @return
     */
    public ServerResponseEntity<ListGagGroupUserResult> getList() {
        try {
            ListGagGroupUserResult result = group.ban.user.getList();
            log.info("group.ban.getList-result: {}", result);
            if (SendUtil.requestSuccess(result)) return success(result);
            return fail(ResponseEnum.EXCEPTION);
        } catch (Exception exception) {
            log.info("group.ban.getList-error: {}", exception.getMessage());
            return fail(ResponseEnum.EXCEPTION);
        }
    }

    /**
     * 移除用户在所有群组中的禁言设置
     *
     * @param groups 群组列表
     * @return
     */
    public ServerResponseEntity<String> remove(List<GroupIdVo> groups) {
        List<GroupMember> groupMembers = BeanUtil.copyToList(groups, GroupMember.class);
        GroupMember[] groupParam = groupMembers.toArray(new GroupMember[groupMembers.size()]);
        GroupModel param = new GroupModel().setMembers(groupParam);
        try {
            Result result = group.ban.user.remove(param);
            log.info("group.ban.remove-result: {}", result);
            if (SendUtil.requestSuccess(result)) return success();
            return fail(ResponseEnum.EXCEPTION);
        } catch (Exception exception) {
            log.info("group.ban.remove-error: {}", exception.getMessage());
            return fail(ResponseEnum.EXCEPTION);
        }
    }

}
